package cn.bocweb.spinning.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.SequenceAnswerBean;
import cn.bocweb.spinning.model.Bean.SequenceKeysBean;
import cn.bocweb.spinning.model.Bean.SequenceSyncForLoginBean;
import cn.bocweb.spinning.model.Bean.SequenceTestBean;
import cn.bocweb.spinning.model.Bean.TestCollectBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.activity.SequenceTestActivity;
import de.greenrobot.event.EventBus;

/**
 * Created by chenchangxing on 16/1/26.
 */
public class SequenceTestFragment extends Fragment implements View.OnClickListener {

    SequenceSyncForLoginBean sequenceSyncForLoginBean;
    boolean isCheckExplainShow = false, isVisible = false, isAnswer = false, isExplainShow = false, isRight = true, isCheckDetail = false, notLoginGetAnswer = false;

    public void setUserid(String userid) {
        this.userid = userid;
    }

    boolean isCollect = false;
    String tid;
    String ttype;
    String userid, libid, allRate, personRate, explain, myWrong, myRight;
    int position, choose = 0, collectPosition = 0, isRightInt = 2;
    UserPresenter presenter;
    SequenceTestBean.DataEntity dataEntity;
    SequenceTestActivity sequenceTestActivity;
    Timer timer;
    SequenceAnswerBean sequenceAnswerBean;
    @Bind(R.id.train_type)
    TextView trainType;
    @Bind(R.id.train_title)
    TextView trainTitle;
    @Bind(R.id.train_title_layout)
    LinearLayout trainTitleLayout;
    @Bind(R.id.train_select_icon_a)
    ImageView trainSelectIconA;
    @Bind(R.id.train_select_answer_a)
    TextView trainSelectAnswerA;
    @Bind(R.id.train_select_a)
    LinearLayout trainSelectA;
    @Bind(R.id.train_select_icon_b)
    ImageView trainSelectIconB;
    @Bind(R.id.train_select_answer_b)
    TextView trainSelectAnswerB;
    @Bind(R.id.train_select_b)
    LinearLayout trainSelectB;
    @Bind(R.id.train_select_icon_c)
    ImageView trainSelectIconC;
    @Bind(R.id.train_select_answer_c)
    TextView trainSelectAnswerC;
    @Bind(R.id.train_select_c)
    LinearLayout trainSelectC;
    @Bind(R.id.train_select_icon_d)
    ImageView trainSelectIconD;
    @Bind(R.id.train_select_answer_d)
    TextView trainSelectAnswerD;
    @Bind(R.id.train_select_d)
    LinearLayout trainSelectD;
    @Bind(R.id.train_select)
    LinearLayout trainSelect;
    @Bind(R.id.train_judge_icon_a)
    ImageView trainJudgeIconA;
    @Bind(R.id.train_judge_answer_a)
    TextView trainJudgeAnswerA;
    @Bind(R.id.train_judge_a)
    LinearLayout trainJudgeA;
    @Bind(R.id.train_judge_icon_b)
    ImageView trainJudgeIconB;
    @Bind(R.id.train_judge_answer_b)
    TextView trainJudgeAnswerB;
    @Bind(R.id.train_judge_b)
    LinearLayout trainJudgeB;
    @Bind(R.id.train_judge)
    LinearLayout trainJudge;
    @Bind(R.id.train_allrate)
    TextView trainAllrate;
    @Bind(R.id.sequence_wrong_people)
    TextView sequenceWrongPeople;
    @Bind(R.id.train_personrate)
    TextView trainPersonrate;
    @Bind(R.id.sequence_wrong_my_wrong)
    TextView sequenceWrongMyWrong;
    @Bind(R.id.sequence_wrong_my_right)
    TextView sequenceWrongMyRight;
    @Bind(R.id.train_explain_rate)
    LinearLayout trainExplainRate;
    @Bind(R.id.train_explain)
    TextView trainExplain;
    @Bind(R.id.train_explain_detail)
    LinearLayout trainExplainDetail;
    @Bind(R.id.sequence_wrong_layout)
    LinearLayout sequenceWrongLayout;
    @Bind(R.id.sequence_explain_explain)
    TextView sequenceExplainExplain;
    @Bind(R.id.sequence_explain_detail)
    LinearLayout sequenceExplainDetail;
    @Bind(R.id.sequence_explain_allrate)
    TextView sequenceExplainAllrate;
    @Bind(R.id.sequence_explain_people_wrong)
    TextView sequenceExplainPeopleWrong;
    @Bind(R.id.sequence_explain_personrate)
    TextView sequenceExplainPersonrate;
    @Bind(R.id.sequence_explain_my_wrong)
    TextView sequenceExplainMyWrong;
    @Bind(R.id.sequence_explain_my_right)
    TextView sequenceExplainMyRight;
    @Bind(R.id.sequence_explain_rate)
    LinearLayout sequenceExplainRate;
    @Bind(R.id.sequence_explain_layout)
    LinearLayout sequenceExplainLayout;


    public SequenceTestFragment(String userid, String libid, int position) {
        this.userid = userid;
        this.libid = libid;
        this.position = position;
    }

    public SequenceSyncForLoginBean getSequenceSyncForLoginBean() {
        return sequenceSyncForLoginBean;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sequence_test, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        /*if ((position + 1) % 49 == 0) {
            isVisible = true;
        } else {*/
        isVisible = isVisibleToUser;
//        }
        if (position == 48) {
            DeviceUtil.Log("isVisible = " + isVisible);
        }
        if (isVisible) {
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
            timer = new Timer();
            presenter = new UserPresenterImp();
            if (!notLoginGetAnswer) {
                presenter.sequenceTest(position, libid, userid);
            } else {
                SequenceAnswerBean bean1 = new SequenceAnswerBean();
                bean1.setChoose(String.valueOf(choose));
                bean1.setIsRight(String.valueOf(isRightInt));
                initType();
                initTitle();
                initOption();
                initInfo(bean1);
                if (!isCheckExplainShow) {
                    trainExplain.setText(explain);
                    trainAllrate.setText(allRate + "%");
                    sequenceWrongPeople.setText(allRate + "%");
                    if (notLoginGetAnswer) {
                        sequenceWrongMyWrong.setText("0");
                        sequenceWrongMyRight.setText("0");
                        trainPersonrate.setText("0%");
                    } else {
                        trainPersonrate.setText(personRate + "%");
                        sequenceWrongMyWrong.setText(myWrong);
                        sequenceWrongMyRight.setText(myRight);
                    }
                    sequenceExplainLayout.setVisibility(View.GONE);
                    sequenceWrongLayout.setVisibility(View.VISIBLE);
                } else {
                    sequenceExplainExplain.setText(explain);
                    sequenceExplainAllrate.setText(allRate + "%");
                    sequenceExplainPeopleWrong.setText(allRate + "%");
                    if (notLoginGetAnswer) {
                        sequenceExplainMyRight.setText("0");
                        sequenceExplainMyWrong.setText("0");
                        sequenceExplainPersonrate.setText("0%");
                    } else {
                        sequenceExplainPersonrate.setText(personRate + "%");
                        sequenceExplainMyWrong.setText(myWrong);
                        sequenceExplainMyRight.setText(myRight);
                    }
                    sequenceExplainLayout.setVisibility(View.VISIBLE);
                    sequenceWrongLayout.setVisibility(View.GONE);
                }
            }
            if (sequenceAnswerBean != null) {
                initInfo(sequenceAnswerBean);
            }
        } else {
            if (EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().unregister(this);
            }
            if (timer != null) {
                timer.cancel();
                timer.purge();
            }

            onPause();
        }
    }

    private void initSelect() {
        trainSelectIconA.setImageResource(R.mipmap.exam_a);
        trainSelectIconB.setImageResource(R.mipmap.exam_b);
        trainSelectIconC.setImageResource(R.mipmap.exam_c);
        trainSelectIconD.setImageResource(R.mipmap.exam_d);
        trainJudgeIconA.setImageResource(R.mipmap.exam_a);
        trainJudgeIconB.setImageResource(R.mipmap.exam_b);
    }


    private void initInfo(SequenceAnswerBean bean) {
        trainJudgeA.setClickable(false);
        trainJudgeB.setClickable(false);
        trainSelectA.setClickable(false);
        trainSelectB.setClickable(false);
        trainSelectC.setClickable(false);
        trainSelectD.setClickable(false);
        String ttype = dataEntity.getTtype();
        if (!(ttype == null || ttype.isEmpty())) {
            switch (Integer.parseInt(ttype)) {
                case 1: {
                    switch (bean.getIsRight()) {
                        case "0": {
                            trainExplainRate.setVisibility(View.VISIBLE);
                            trainAllrate.setText(String.valueOf(bean.getAllrate()));
                            trainPersonrate.setText(String.valueOf(bean.getPersonrate()));

                            trainExplainDetail.setVisibility(View.VISIBLE);
                            trainExplain.setText(bean.getExplain());

                            switch (dataEntity.getRight()) {

                                case "1": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "2": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "3": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "4": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                            break;
                        }
                        case "1": {
                            switch (dataEntity.getRight()) {
                                case "1": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "2": {
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "3": {
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "4": {
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                    break;
                }
                case 2: {
                    switch (bean.getIsRight()) {
                        case "0": {
                            switch (bean.getChoose()) {
                                case "1": {
                                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "2": {
                                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                            break;
                        }
                        case "1": {
                            switch (bean.getChoose()) {
                                case "1": {
                                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "2": {
                                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        isVisible = false;
    }

    @Override
    public void onStop() {
        super.onStop();
        if (timer != null) {
            timer.cancel();
            timer.purge();
        }
        EventBus.getDefault().unregister(this);
    }

    /**
     * 获取题目
     */
    public void onEventMainThread(SequenceTestBean bean) {
        //判断 isVisible 是因为 viewpager 会一次性加载三个 Fragment, 所以要判断 Fragmnet 是不是 已显示
        if (isVisible) {
            if (bean.getReturn_code() == 0) {
                tid = bean.getData().getId();
                ttype = bean.getData().getTtype();
                if (sequenceTestActivity == null) {
                    sequenceTestActivity = (SequenceTestActivity) getActivity();
                }
                sequenceTestActivity.isCollect(bean.getRecord().getIsCollect().equals("1"));
                isCollect = bean.getRecord().getIsCollect().equals("1");

                this.dataEntity = bean.getData();

                initType();//判断是选择题还是判断题
                initTitle();//显示题目
                initOption();//显示选项

                allRate = bean.getRecord().getAllrate();

                if (bean.getRecord().getPersonrate() != null) {
                    personRate = bean.getRecord().getPersonrate();
                } else {
                    personRate = "0";
                }
                explain = bean.getRecord().getExplain();
                if (bean.getRecord().getWrong_times() != null) {
                    myWrong = bean.getRecord().getWrong_times();
                } else {
                    myWrong = "0";
                }
                if (bean.getRecord().getOk_times() != null) {
                    myRight = bean.getRecord().getOk_times();
                } else {
                    myRight = "0";
                }

                if (userid != null && !userid.isEmpty()) {
                    if (!bean.getRecord().getChoose().equals("0")) {
                        isAnswer = true;
                    } else {
                        isAnswer = false;
                    }
                }

                if (!isAnswer) {
                    initClickBtn();
                    initSelect();
                    sequenceExplainLayout.setVisibility(View.GONE);
                    sequenceWrongLayout.setVisibility(View.GONE);
                } else {
                    if (isVisible) {
                        SequenceAnswerBean bean1 = new SequenceAnswerBean();
                        bean1.setChoose(bean.getRecord().getChoose());
                        bean1.setIsRight(bean.getRecord().getIsright());
                        initInfo(bean1);

                        if (!isCheckExplainShow) {
                            trainExplain.setText(explain);
                            trainAllrate.setText(allRate + "%");
                            sequenceWrongPeople.setText(allRate + "%");
                            if (notLoginGetAnswer) {
                                sequenceWrongMyWrong.setText("0");
                                sequenceWrongMyRight.setText("0");
                                trainPersonrate.setText("0%");
                            } else {
                                trainPersonrate.setText(personRate + "%");
                                sequenceWrongMyWrong.setText(myWrong);
                                sequenceWrongMyRight.setText(myRight);
                            }
                            sequenceExplainLayout.setVisibility(View.GONE);
                            sequenceWrongLayout.setVisibility(View.VISIBLE);
                        } else {
                            sequenceExplainExplain.setText(explain);
                            sequenceExplainAllrate.setText(allRate + "%");
                            sequenceExplainPeopleWrong.setText(allRate + "%");
                            if (notLoginGetAnswer) {
                                sequenceExplainMyRight.setText("0");
                                sequenceExplainMyWrong.setText("0");
                                sequenceExplainPersonrate.setText("0%");
                            } else {
                                sequenceExplainPersonrate.setText(personRate + "%");
                                sequenceExplainMyWrong.setText(myWrong);
                                sequenceExplainMyRight.setText(myRight);
                            }
                            sequenceExplainLayout.setVisibility(View.VISIBLE);
                            sequenceWrongLayout.setVisibility(View.GONE);
                        }
                    }
                }
            } else {
                DeviceUtil.MyToast(getActivity(), bean.getMsg());
            }
        }
    }


    /**
     * 获取答案
     */
    public void onEventMainThread(SequenceKeysBean bean) {
        if (isVisible) {
            if (bean.getReturn_code() == 0) {

                sequenceSyncForLoginBean = new SequenceSyncForLoginBean();
                sequenceSyncForLoginBean.setChoose(String.valueOf(choose));
                sequenceSyncForLoginBean.setCount(String.valueOf(position + 1));
                String isright = isRight ? "1" : "0";
                sequenceSyncForLoginBean.setIsright(isright);
                sequenceSyncForLoginBean.setTid(dataEntity.getId());
                sequenceSyncForLoginBean.setTtype(dataEntity.getTtype());

                allRate = bean.getData().getAllrate();
                if (bean.getData().getPersonrate() != null) {
                    personRate = bean.getData().getPersonrate();
                } else {
                    personRate = "0";
                }
                explain = bean.getData().getExplain();

                if (myRight != null) {
                    myRight = bean.getData().getOk_times();
                } else {
                    myRight = "0";
                }

                if (myWrong != null) {
                    myWrong = bean.getData().getWrong_times();
                } else {
                    myWrong = "0";
                }

                if (isVisible) {
                    if (userid == null || userid.isEmpty()) {
                        notLoginGetAnswer = true;
                        personRate = "0";
                        myRight = "0";
                        myWrong = "0";
                    }
                }

                initExplain(bean.getData());
                initRate(bean.getData());

                sequenceExplainLayout.setVisibility(View.GONE);
                sequenceWrongLayout.setVisibility(View.VISIBLE);

                if (!isRight) {
                    SequenceAnswerBean sequenceAnswerBean = new SequenceAnswerBean();
                    sequenceAnswerBean.setPersonrate(bean.getData().getPersonrate());
                    sequenceAnswerBean.setAllrate(bean.getData().getAllrate());
                    sequenceAnswerBean.setExplain(bean.getData().getExplain());
                    sequenceAnswerBean.setChoose(dataEntity.getRight());
                    sequenceAnswerBean.setIsRight(String.valueOf(isRightInt));
                    if (sequenceTestActivity == null) {
                        sequenceTestActivity = (SequenceTestActivity) getActivity();
                    }
                    sequenceTestActivity.addQuizDetail(sequenceAnswerBean, position);
                } else {
                    SequenceAnswerBean sequenceAnswerBean = new SequenceAnswerBean();
                    sequenceAnswerBean.setChoose(String.valueOf(choose));
                    sequenceAnswerBean.setIsRight(String.valueOf(isRightInt));

                    if (sequenceTestActivity == null) {
                        sequenceTestActivity = (SequenceTestActivity) getActivity();
                    }
                    sequenceTestActivity.addQuizDetail(sequenceAnswerBean, position);
                }

                if (isCheckDetail) {
                    initExplain(bean.getData());
                    sequenceExplainLayout.setVisibility(View.VISIBLE);
                }
            } else {
                DeviceUtil.MyToast(getActivity(), bean.getMsg());
            }
        }
    }

    /**
     * 收藏题目
     */
    public void onEventMainThread(TestCollectBean bean) {
        if (position == collectPosition) {
            if (sequenceTestActivity == null) {
                sequenceTestActivity = (SequenceTestActivity) getActivity();
            }
            sequenceTestActivity.isCollect(true);
            DeviceUtil.MyToast(getActivity(), bean.getMsg());
            collectPosition = -1;
        }
    }


    private void initClickBtn() {
        trainSelectA.setOnClickListener(this);
        trainSelectB.setOnClickListener(this);
        trainSelectC.setOnClickListener(this);
        trainSelectD.setOnClickListener(this);

        trainJudgeA.setOnClickListener(this);
        trainJudgeB.setOnClickListener(this);
    }


    private void initRate(SequenceKeysBean.DataEntity dataEntity) {
        trainAllrate.setText(String.valueOf(allRate) + "%");
        trainPersonrate.setText(String.valueOf(personRate) + "%");
        sequenceWrongMyRight.setText(String.valueOf(myRight));
        sequenceWrongMyWrong.setText(String.valueOf(myWrong));
        sequenceWrongPeople.setText(String.valueOf(allRate) + "%");
    }

    private void initExplain(SequenceKeysBean.DataEntity dataEntity) {
        trainExplain.setText(dataEntity.getExplain());
    }

    /**
     * 判断是选择题还是判断题
     */
    private void initType() {
        String ttype = dataEntity.getTtype();
        if (!(ttype == null || ttype.isEmpty())) {
            switch (Integer.parseInt(ttype)) {
                case 1: {
                    trainType.setText("选");
                    trainSelect.setVisibility(View.VISIBLE);
                    trainJudge.setVisibility(View.GONE);
                    break;
                }
                case 2: {
                    trainType.setText("判");
                    trainSelect.setVisibility(View.GONE);
                    trainJudge.setVisibility(View.VISIBLE);
                    break;
                }
            }
        }
    }

    /**
     * 显示题目
     */
    private void initTitle() {
        trainTitle.setText(dataEntity.getTitle());
        trainTitleLayout.setVisibility(View.VISIBLE);
    }


    private void initOption() {
        if (!(dataEntity.getOption() == null || dataEntity.getOption().isEmpty())) {
            trainSelectAnswerA.setText(dataEntity.getOption().get(0));
            trainSelectAnswerB.setText(dataEntity.getOption().get(1));
            trainSelectAnswerC.setText(dataEntity.getOption().get(2));
            trainSelectAnswerD.setText(dataEntity.getOption().get(3));
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        isVisible = false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.train_select_a: {
                initSelectResult(1);
                choose = 1;
                break;
            }
            case R.id.train_select_b: {
                initSelectResult(2);
                choose = 2;
                break;
            }
            case R.id.train_select_c: {
                initSelectResult(3);
                choose = 3;
                break;
            }
            case R.id.train_select_d: {
                initSelectResult(4);
                choose = 4;
                break;
            }
            case R.id.train_judge_a: {
                initJudgeResult(1);
                choose = 1;
                break;
            }
            case R.id.train_judge_b: {
                initJudgeResult(2);
                choose = 2;
                break;
            }
        }
    }

    private void initSelectResult(int result) {
        isAnswer = true;
        trainSelectA.setClickable(false);
        trainSelectB.setClickable(false);
        trainSelectC.setClickable(false);
        trainSelectD.setClickable(false);
        trainJudgeA.setClickable(false);
        trainJudgeB.setClickable(false);

        int selectAnswer = Integer.parseInt(dataEntity.getRight());
        if (result == selectAnswer) {
            isRightInt = 1;
            if (sequenceTestActivity == null) {
                sequenceTestActivity = (SequenceTestActivity) getActivity();
                sequenceTestActivity.addQuizCorrect(dataEntity.getTtype(), dataEntity.getId(), String.valueOf(selectAnswer), true, position);

            } else {
                sequenceTestActivity.addQuizCorrect(dataEntity.getTtype(), dataEntity.getId(), String.valueOf(result), true, position);
            }
            isRight = true;

            presenter.sequenceKeys(position, dataEntity.getId(), dataEntity.getTtype(), libid, "1", String.valueOf(result), userid);
            DeviceUtil.MyToast(getActivity(), Constant.ANSWER_RIGHT);
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    sequenceTestActivity.selectRightAnswer(position + 1);
                }
            };

            timer.schedule(timerTask, 1000);
            switch (result) {
                case 1: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 2: {
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 3: {
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 4: {
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
            }
        } else {
            isRightInt = 0;
            isRight = false;

            presenter.sequenceKeys(position, dataEntity.getId(), dataEntity.getTtype(), libid, "0", String.valueOf(result), userid);
            trainExplainRate.setVisibility(View.VISIBLE);
            trainExplainDetail.setVisibility(View.VISIBLE);

            if (sequenceTestActivity == null) {
                sequenceTestActivity = (SequenceTestActivity) getActivity();
            }

            sequenceTestActivity.addQuizCorrect(dataEntity.getTtype(), dataEntity.getId(), String.valueOf(selectAnswer), false, position);
            switch (selectAnswer) {
                case 1: {
                    drawSelectResult(trainSelectIconA);
                    break;
                }
                case 2: {
                    drawSelectResult(trainSelectIconB);
                    break;
                }
                case 3: {
                    drawSelectResult(trainSelectIconC);
                    break;
                }
                case 4: {
                    drawSelectResult(trainSelectIconD);
                    break;
                }
            }
        }
    }

    private void drawSelectResult(ImageView select) {
        trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
        trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
        trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
        trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);

        select.setImageResource(R.mipmap.exam_answer_right);
    }


    private void initJudgeResult(int result) {
        isAnswer = true;
        trainJudgeA.setClickable(false);
        trainJudgeB.setClickable(false);
        trainSelectA.setClickable(false);
        trainSelectB.setClickable(false);
        trainSelectC.setClickable(false);
        trainSelectD.setClickable(false);
        int judgeAnswer = Integer.parseInt(dataEntity.getRight());
        if (result == judgeAnswer) {
            isRightInt = 1;
            isRight = true;
            DeviceUtil.MyToast(getActivity(), Constant.ANSWER_RIGHT);
            if (sequenceTestActivity == null) {
                sequenceTestActivity = (SequenceTestActivity) getActivity();
                sequenceTestActivity.addQuizCorrect(dataEntity.getTtype(), dataEntity.getId(), String.valueOf(result), true, position);

            } else {
                sequenceTestActivity.addQuizCorrect(dataEntity.getTtype(), dataEntity.getId(), String.valueOf(result), true, position);
            }
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    sequenceTestActivity.selectRightAnswer(position + 1);

                }
            };
            timer.schedule(timerTask, 1000);
            presenter.sequenceKeys(position, dataEntity.getId(), dataEntity.getTtype(), libid, "1", String.valueOf(result), userid);
            switch (result) {
                case 1: {
                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 2: {
                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
            }
        } else {
            isRightInt = 0;
            isRight = false;
            trainExplainRate.setVisibility(View.VISIBLE);
            trainExplainDetail.setVisibility(View.VISIBLE);
            if (sequenceTestActivity != null) {
                sequenceTestActivity.addQuizCorrect(dataEntity.getTtype(), dataEntity.getId(), String.valueOf(result), false, position);
            } else {
                sequenceTestActivity = (SequenceTestActivity) getActivity();
                sequenceTestActivity.addQuizCorrect(dataEntity.getTtype(), dataEntity.getId(), String.valueOf(result), false, position);

            }
            presenter.sequenceKeys(position, dataEntity.getId(), dataEntity.getTtype(), libid, "0", String.valueOf(result), userid);
            switch (judgeAnswer) {
                case 1: {
                    drawJudgeRuselt(trainJudgeIconA);
                    break;
                }
                case 2: {
                    drawJudgeRuselt(trainJudgeIconB);
                    break;
                }
            }
        }
    }

    public void showDetail() {
        sequenceExplainExplain.setText(explain);
        sequenceExplainPersonrate.setText(personRate + "%");
        sequenceExplainAllrate.setText(allRate + "%");
        sequenceExplainMyWrong.setText(myWrong);
        sequenceExplainMyRight.setText(myRight);
        sequenceExplainPeopleWrong.setText(allRate + "%");

        sequenceWrongLayout.setVisibility(View.GONE);
        sequenceExplainLayout.setVisibility(View.VISIBLE);

        isCheckExplainShow = !isCheckExplainShow;
    }

    private void drawJudgeRuselt(ImageView judge) {
        trainJudgeIconA.setImageResource(R.mipmap.exam_answer_wrong);
        trainJudgeIconA.setImageResource(R.mipmap.exam_answer_wrong);

        judge.setImageResource(R.mipmap.exam_answer_right);
    }

    public void setCollect(int i) {
        if (i == position) {
            collectPosition = i;
            if (dataEntity != null) {
                presenter.testCollect(dataEntity.getId(), dataEntity.getTtype(), libid, userid, String.valueOf("0"), String.valueOf(choose));
            } else {
                DeviceUtil.MyToast(getActivity(), "还未获取题目");
            }
        }
    }

    public void setVisible(boolean visible) {

        isVisible = visible;
        if (isVisible) {
            timer = new Timer();
            presenter = new UserPresenterImp();
            if (!notLoginGetAnswer) {
                presenter.sequenceTest(position, libid, userid);
            } else {
                SequenceAnswerBean bean1 = new SequenceAnswerBean();
                bean1.setChoose(String.valueOf(choose));
                bean1.setIsRight(String.valueOf(isRightInt));
                initType();
                initTitle();
                initOption();
                initInfo(bean1);
                if (!isCheckExplainShow) {
                    trainExplain.setText(explain);
                    trainAllrate.setText(allRate + "%");
                    sequenceWrongPeople.setText(allRate + "%");
                    if (notLoginGetAnswer) {
                        sequenceWrongMyWrong.setText("0");
                        sequenceWrongMyRight.setText("0");
                        trainPersonrate.setText("0%");
                    } else {
                        trainPersonrate.setText(personRate + "%");
                        sequenceWrongMyWrong.setText(myWrong);
                        sequenceWrongMyRight.setText(myRight);
                    }
                    sequenceExplainLayout.setVisibility(View.GONE);
                    sequenceWrongLayout.setVisibility(View.VISIBLE);
                } else {
                    sequenceExplainExplain.setText(explain);
                    sequenceExplainAllrate.setText(allRate + "%");
                    sequenceExplainPeopleWrong.setText(allRate + "%");
                    if (notLoginGetAnswer) {
                        sequenceExplainMyRight.setText("0");
                        sequenceExplainMyWrong.setText("0");
                        sequenceExplainPersonrate.setText("0%");
                    } else {
                        sequenceExplainPersonrate.setText(personRate + "%");
                        sequenceExplainMyWrong.setText(myWrong);
                        sequenceExplainMyRight.setText(myRight);
                    }
                    sequenceExplainLayout.setVisibility(View.VISIBLE);
                    sequenceWrongLayout.setVisibility(View.GONE);
                }
            }
            if (sequenceAnswerBean != null) {
                initInfo(sequenceAnswerBean);
            }
        } else {
            if (timer != null) {
                timer.cancel();
                timer.purge();
            }
        }
    }

    public String getTid() {
        return tid;
    }

    public String getTtype() {
        return ttype;
    }

    public boolean isCollect() {
        return isCollect;
    }
}
